


#include "../include/search.h"
#include <stdlib.h>

int bisearch(void *sorted, int size, int esize, const void *target, int (*compare)(const void *key1, const void *key2))
{
	int left = 0, right = size - 1;

	while (left <= right)
	{
		int mid = (left + right) / 2;
		switch (compare((char *)sorted + (esize * mid), target))
		{
			case -1:/**/
				left = mid + 1;
			break;
			case 0:
				return mid;
			case 1:
				right = mid - 1;
				break;
			default:
				break;
		}
	}

	return -1; //not found
}


